package com.bw.controller;

import com.alibaba.fastjson.JSONObject;
import com.bw.pojo.Goods;
import com.bw.pojo.vo.GoodsVo;
import com.bw.service.impl.GoodsServiceImpl;
import com.bw.util.PageResult;
import com.github.pagehelper.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/goods/*")
public class GoodsController extends BaseController{
    GoodsServiceImpl service = new GoodsServiceImpl();
    //分页
    protected void getGoodsList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数 2.将json转对象 3.将对象丢给Service 4.响应用户
        String reqInfos = req.getParameter("reqInfos");
        GoodsVo goodsVo = JSONObject.parseObject(reqInfos, GoodsVo.class);
        if(null != goodsVo && goodsVo.getPageNum() == null){
            resp.getWriter().println(JSONObject.toJSONString(4000));
            return;
        }
        if(null != goodsVo && goodsVo.getPageSize()==null){
            resp.getWriter().println(JSONObject.toJSONString(400));
            return;
        }
        //返回 分页工具类对象
        PageInfo<GoodsVo> pageInfo = service.getGoodsList(goodsVo);
        resp.getWriter().println(JSONObject.toJSONString(pageInfo));
    }
    //删除
    protected void getToDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数 2.将json转对象 3.将对象丢给Service 4.响应用户
        String reqInfos = req.getParameter("reqInfos");
        GoodsVo goodsVo = JSONObject.parseObject(reqInfos, GoodsVo.class);
//        service.getToDel(goodsVo);
//        resp.getWriter().println(JSONObject.toJSONString(200));
    }
    //添加
    protected void getGoodsAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String reqInfos = req.getParameter("reqInfos");
        Goods goods = JSONObject.parseObject(reqInfos, Goods.class);
        service.getGoodsAdd(goods);
        resp.getWriter().println(JSONObject.toJSONString(200));
    }
    //修改
    protected void getGoodsUpd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String reqInfos = req.getParameter("reqInfos");
        Goods goods = JSONObject.parseObject(reqInfos, Goods.class);
        service.getGoodsUpd(goods);
        resp.getWriter().println(JSONObject.toJSONString(200));
    }
    //根据供应商id 获取商品信息
    protected void findGoodsBySupplierId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String reqInfos = req.getParameter("reqInfos");
        Goods goods = JSONObject.parseObject(reqInfos, Goods.class);
        List<Goods> list = service.findGoodsBySupplierId(goods.getSupplierId());
        resp.getWriter().println(JSONObject.toJSONString(list));
    }


    protected void getGoodsSupplierId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String reqInfos = req.getParameter("reqInfos");
        Goods goods = JSONObject.parseObject(reqInfos, Goods.class);
        List<Goods> list = service.getGoodsSupplierId(goods.getSupplierId());
        resp.getWriter().println(JSONObject.toJSONString(list));
    }
}

